from PyQt5.QtWidgets import *
from .home import *


class HomeUI(Ui_Home):
    def __init__(self):
        self.sopenHandle = ''
        self.scloseHandle = ''
        self.backHandle = ''
        self.nextHandle = ''
        self.refreshHandle = ''
        self.searchHandle = ''
        self.textChangeHandle = 'textChangeHandle'

    def init(self):
        self.closePB.setEnabled(False)
        # self.portCB.setEditable(True)

        try:
            with open("./config/serial.config", "r") as fp:
                data = fp.read().split(",")
                port = data[0]
                bps = data[1]
                timeout = data[2]
                self.portCB.setCurrentText(str(port))
                self.bpsCB.setCurrentText(str(bps))
                self.timeoutLE.setText(str(timeout))
        except Exception as e:
            print(e)

        # TODO 优化 2 设置水平方向表格为自适应的伸缩模式
        # self.cardsTW.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)

        # TODO 优化3 将表格变为禁止编辑
        self.cardsTW.setEditTriggers(QAbstractItemView.NoEditTriggers)

        # TODO 优化 4 设置表格整行选中
        self.cardsTW.setSelectionBehavior(QAbstractItemView.SelectRows)

        # TODO 优化 5 将行与列的高度设置为所显示的内容的宽度高度匹配
        QTableWidget.resizeColumnsToContents(self.cardsTW)
        QTableWidget.resizeRowsToContents(self.cardsTW)

        # TODO 优化 6 表格头的显示与隐藏
        # self.cardsTW.verticalHeader().setVisible(False)
        # self.cardsTW.horizontalHeader().setVisible(False)

        self.searchtextLE.setFocus()

    def sopen(self):
        self.sopenHandle()

    def sclose(self):
        self.scloseHandle()

    def back(self):
        self.backHandle()

    def next(self):
        self.nextHandle()

    def refresh(self, i):
        self.refreshHandle(i)

    def search(self):
        self.searchHandle()

    def textChange(self, str):
        self.textChangeHandle(str)


